package com.guigu.www.erp.dengtao.controller;

import com.guigu.www.erp.common.pojo.DFile;
import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.common.pojo.SCell;
import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.dengtao.service.DTDFileService;
import com.guigu.www.erp.dengtao.service.SCellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/Kucun")
public class AnQuanKuCun {
    @Autowired
    private DTDFileService dFileService;
    @Autowired
    private SCellService sCellService;

    @PostMapping("aqkcpesjdcx")
    public PageVo<DFile> aqkczd(@RequestBody Map<String, String> map) {
        return dFileService.pagedfkuc(map);
    }

    @PostMapping("addaqpe")
    public Integer addpez(@RequestBody SCell sCell) {

        Calendar now = Calendar.getInstance();
        String time = "400";
//年
        time += now.get(Calendar.YEAR);
//月
        String month = (now.get(Calendar.MONTH) + 1) + "";
        month = month.length() == 1 ? "0" + month : month;
        time += month;

//日
        String day = now.get(Calendar.DAY_OF_MONTH) + "";
        day = day.length() == 1 ? "0" + day : day;
        time += day;

        //最大编号
        String s = sCellService.select();
        if (s == null) {
            time += "0001";
        } else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str = i + "";
            if (str.length() == 1) {
                str = "000" + str;
            } else if (str.length() == 2) {
                str = "00" + str;
            } else if (str.length() == 3) {
                str = "0" + str;
            }
            time += str;
        }

        sCell.setStoreId(time);
        sCell.setCheckTag("0");

        dFileService.upxg(sCell.getProductId());

        sCellService.add(sCell);



        return 0;
    }

    @PostMapping("aqkcpzfh")
    public PageVo<SCell> aqkzpzfh(@RequestBody Map<String, String> map, HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser.getRole().getRoleId()==4){
            return null;
        }
        return sCellService.selctfh(map);
    }

    @PutMapping("tongguo/{id}")
    public void fhtg(@PathVariable String id) {
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        Date date=new Date();
        String format1=format.format(date);


        sCellService.tg(id,format1);
    }

    @PutMapping("butongguo/{id}")
    public void fhbtg(@PathVariable String id) {
        sCellService.btg(id);
    }

    @PostMapping("aqkcpzbg")
    public PageVo<SCell> bg(@RequestBody Map<String, String> map) {
        return sCellService.selctbg(map);
    }

    @PutMapping("xgkc")
    public void xiug(@RequestBody SCell sCell) {
        sCellService.put(sCell);
    }

    @PostMapping("cxsy")
    public PageVo<SCell> cxsy(@RequestBody Map<String, String> map) {
        return sCellService.cxsy(map);
    }
}
